package com.example.springbootdemo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;

/**
 * @author H
 * @time 2022/3/14 16:36
 */
@Controller
public class ThymleafController {

    @RequestMapping("/test/index")
    public String index(HttpServletRequest request, HttpSession session){
        // 后台存储数据，给页面提供，让页面渲染
        request.setAttribute("name", "李华");

        // 存储集合数据
        ArrayList<String> citys = new ArrayList<>();
        citys.add("北京");
        citys.add("上海");
        citys.add("广州");
        citys.add("深圳");
        request.setAttribute("citys", citys);

        // 存储一个年龄
        request.setAttribute("age", 17);

        // 存储session数据
        session.setAttribute("address", "中国深圳");

        request.setAttribute("phone", "<span style=color:red>小米</span>手机");

        request.setAttribute("count", 10);

        // 返回视图名称，默认视图在templates
        return "index";
    }

    @RequestMapping("/test/count")
    public String count(HttpServletRequest request, Integer count){
        System.out.println("获取到的count为：" + count);
        request.setAttribute("count", count);
        return "count";
    }

}
